#include "dragdockwidget.hpp"

DragDockWidget::DragDockWidget() {
	setFeatures(DragDockWidget::NoDockWidgetFeatures);

	// Allocation Dynamique
	QLabel *dk = new QLabel;
	QLabel *ostrich = new QLabel;
	_tab = new QTabWidget;
	_frame_character = new QFrame;
	_frame_monster = new QFrame;
	_frame_object = new QFrame;
	_character = new QVBoxLayout;
	_monster = new QVBoxLayout;
	_object = new QVBoxLayout;

	// Widget du Dock
	setWidget(_tab);

	// Tab Personnage
	_tab->addTab(_frame_character, "Personnage");
	_frame_character->setLayout(_character);
	_character->addStretch();

	dk->setPixmap(QPixmap("DonkeyKongLeft.png"));
	_character->addWidget(dk);
	dk->show();

	ostrich->setPixmap(QPixmap("autruche.png"));
	_character->addWidget(ostrich);
	ostrich->show();

	// Tab Monstre
	_tab->addTab(_frame_monster, "Monstre");
	_frame_monster->setLayout(_monster);
	_monster->addStretch();

	// Tab Objet
	_tab->addTab(_frame_object, "Objet");
	_frame_object->setLayout(_object);
	_object->addStretch();

   //On accepte les Drop
	setAcceptDrops(false);
}

void DragDockWidget::dragEnterEvent(QDragEnterEvent *event) {
	if(event->mimeData()->hasFormat("application/sprite"))  {
		if(event->source() == this) {
			event->setDropAction(Qt::MoveAction);
			event->accept();
		}
		else event->acceptProposedAction();

	}
	else event->ignore();

}

void DragDockWidget::dragMoveEvent(QDragMoveEvent *event) {
	if(event->mimeData()->hasFormat("application/sprite")) {
		if(event->source() == this) {
			event->setDropAction(Qt::MoveAction);
			event->accept();
		}
		else event->acceptProposedAction();

	}
	else event->ignore();
}

void DragDockWidget::mousePressEvent(QMouseEvent *event) {
	if(event->button() == Qt::RightButton)
		return;

	QLabel *child = static_cast<QLabel*>(childAt(event->pos()));
	if(!child)
		return;

	if(_tab == childAt(event->pos()) || _frame_character == childAt(event->pos()) ||
			_frame_monster == childAt(event->pos()) || _frame_object == childAt(event->pos()))
		return;

	QPixmap pixmap = *child->pixmap();

	QByteArray itemData;
	QDataStream dataStream(&itemData, QIODevice::WriteOnly);

	dataStream << pixmap << QPoint(event->pos() - child->pos());

	QMimeData *mimeData = new QMimeData;
	mimeData->setData("application/sprite", itemData);

	QDrag *drag = new QDrag(this);
	drag->setPixmap(pixmap);
	drag->setMimeData(mimeData);
	drag->setHotSpot(event->pos() - child->pos());

	QPixmap tempPixmap = pixmap;
	QPainter painter;
	painter.begin(&tempPixmap);
	painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
	painter.end();

	child->setPixmap(tempPixmap);

	if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction)
		child->close();
	else {
		child->show();
		child->setPixmap(pixmap);
	}
}

void DragDockWidget::dropEvent(QDropEvent *event) {
	if(event->mimeData()->hasFormat("application/sprite")) {
		QByteArray itemData = event->mimeData()->data("application/sprite");
		QDataStream dataStream(&itemData, QIODevice::ReadOnly);

		QPixmap pixmap;
		QPoint offset;

		dataStream >> pixmap >> offset;

		QLabel *img = new QLabel(this);
		img->setPixmap(pixmap);
		img->move(event->pos() - offset);
		img->show();
		img->setAttribute(Qt::WA_DeleteOnClose);

		if(event->source() == this) {
			event->setDropAction(Qt::IgnoreAction);
			event->accept();
		}
		else event->acceptProposedAction();
	}
	else event->ignore();
}
